/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Parser;
import compiler.CMinusCompiler;
import java.util.HashMap;
import lowlevel.BasicBlock;
import lowlevel.CodeItem;
import lowlevel.Data;
import lowlevel.Function;
import scanner.*;
import scanner.Token.TokenType;

/**
 *
 * @author Fabulous
 */
public class Vardecl extends Decl{
    private int arraySize; //optional array size
    public Vardecl(Identifier id, TokenType type){
        super(id, TokenType.INT_TOKEN);
        arraySize = -1;
        
        if(type != TokenType.INT_TOKEN){
            throw new ParseException("Error creating vardecle. Type is not Int.");
        }
    }
    public void setArraySize(int size){
        arraySize = size;
    }

    @Override
    void print(String indent) {
        CMinusParser.oFile.println(indent + "VarDecl:");
        CMinusParser.oFile.println(indent + "  int");
        this.id.print(indent + "  ");
        
        if(arraySize != -1){
            CMinusParser.oFile.println(indent + "  [" + arraySize + "]");
        }
        CMinusParser.oFile.flush();
    }

    @Override
    public CodeItem genLLCode() {
        Data global = new Data(Data.TYPE_INT, id.idName);
        CMinusCompiler.globalHash.put(id.idName, id.idName);
        return global;
    }
    
    //local decls - give them a register number and add to the symbol table
    public void genLLCode(Function func){
        int regNum = func.getNewRegNum();
        HashMap symTable = func.getTable();
        symTable.put(id.idName, regNum);
    }
}